package com.phantasy.modules.scene.cases
{
	import com.phantasy.modules.BaseModule;
	import com.phantasy.modules.scene.SceneDataManager;
	import com.phantasy.net.SocketCommand;
	import com.phantasy.net.connection.Connection;
	import com.phantasy.proto.m_map_enter_toc;
	import com.phantasy.proto.m_map_enter_tos;
	import com.phantasy.scene.GameScene;
	import com.phantasy.scene.WorldManager;
	import com.phantasy.scene.sceneData.MapDataVo;
	import com.phantasy.scene.sceneKit.LoadingSetter;
	import com.phantasy.scene.sceneManager.UnitPool;
	import com.phantasy.scene.sceneUnit.MyRole;
	import com.phantasy.scene.sceneUnit.RoleActState;
	import com.phantasy.scene.sceneUnit.map.Map;
	import com.phantasy.scene.sceneUtils.Slice;
	
	public class MapCase extends BaseModule
	{
		private static var _instance:MapCase;

		public var isFirstEnterMap:Boolean = true;
		public function MapCase()
		{
			super();
		}
		
		public static function getInstance():MapCase
		{
			if (_instance == null)
			{
				_instance = new MapCase();
			}
			return _instance;
		}
		
		public function get view():GameScene
		{
			return GameScene.getInstance();
		}
		
		/**
		 *进入地图 
		 * @param mapID
		 * 
		 */		
		public function toEnter(mapID:int):void
		{
			LoadingSetter.mapLoading(true, 0.99, "地图加载完毕，正在请求数据");
			SceneDataManager.isGaming = true;
			var mapEnter:m_map_enter_tos = new m_map_enter_tos();
			mapEnter.mapid = mapID;
			sendSocketMessage(mapEnter);
		}
		
		override protected function initListeners():void
		{
			addSocketListener(SocketCommand.MAP_ENTER, this.onEnter);
		}
		
		private function onEnter(message:m_map_enter_toc):void
		{
			var mapDataVo:MapDataVo;
			var myRole:MyRole;
			if (SceneDataManager.isGaming == false)
			{
				return;
			}
			
			if(Connection.getInstance().isReConnect){
				
			}			
			
			if(message&&message.succ){
				
				if(message.return_self){
					mapDataVo = WorldManager.getMapDataVo(message.map_id);
					SceneDataManager.setMapData(mapDataVo);
					Slice.offsetx = mapDataVo.offsetX;
					Slice.offsety = mapDataVo.offsetY;
					this.view.clean();
					this.view.reset();
					LoadingSetter.mapLoading(false);
					this.view.map.startLoadBlur();
				}
				
				if(message.return_self){
					if(this.view.hero==null){
						myRole = UnitPool.getMyRole();
						myRole.reset(message.role_map_info);
						this.view.addUnit(myRole, message.role_map_info.pos.tx, message.role_map_info.pos.ty);
						this.view.visible=true;
						this.view.hero = myRole;
						switch (message.role_map_info.state)
						{
							case RoleActState.DEAD:
								myRole.die();
								break;
						}
						Map.isCanLoadedMap=true;
						
						this.view.centerCamera(myRole.x, myRole.y, true);
					}
				}
				
			}else{
				throw new Error("进入地图出错"+message.reason+"------------"+message.error_no);
			}
		}

	}
}